package com.xiaofengstu.mcams;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author FENGZENG
 * @ClassName CodeGenerator
 * @date 2021/10/13 1:04
 * @Description TODO
 */
public class CodeGenerator {

  static final String  dbUrl = "jdbc:mysql://localhost:3306/mcams?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
  static final String javaDir = "\\src\\main\\java";

  public static void main(String[] args) {
    List<String> tables = new ArrayList<>();

    //添加要生成的表的列表
    //tables.add("t_asset");
    //tables.add("t_user");
    //tables.add("t_campus");
    tables.add("t_log");
    //tables.add("t_menu");
    //tables.add("t_role");
    //tables.add("t_type");
    //tables.add("workflow");
    //tables.add("t_dept");

    FastAutoGenerator.create(dbUrl,
            "root", "root")
        .globalConfig(builder -> {
          builder.author("fengzeng")               //作者
              .outputDir(System.getProperty("user.dir") + javaDir)    //输出路径(写到java目录)
              //.enableSwagger()           //开启swagger
              .commentDate("yyyy-MM-dd")
              .fileOverride();            //开启覆盖之前生成的文件

        })
        .packageConfig(builder -> {
          builder.parent("com.xiaofengstu.mcams")
              .moduleName("web")
              .entity("entity")
              .service("service")
              .serviceImpl("service.impl")
              .controller("controller")
              .mapper("mapper")
              .xml("mapper")
              .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "\\src\\main\\resources\\mapper"));
        })
        .strategyConfig(builder -> {
          builder.addInclude(tables)
              //.addTablePrefix("p_")
              .serviceBuilder()
              .formatServiceFileName("%sService")
              .formatServiceImplFileName("%sServiceImpl")
              .entityBuilder()
              .enableLombok()
              .logicDeleteColumnName("deleted")
              .enableTableFieldAnnotation()
              .controllerBuilder()
              .formatFileName("%sController")
              .enableRestStyle()
              .mapperBuilder()
              .superClass(BaseMapper.class)
              .formatMapperFileName("%sMapper")
              .enableMapperAnnotation()
              .formatXmlFileName("%sMapper");
        })
        .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
        .execute();
  }
}
